import msg from "./msg"

export const formatTime = (date: Date) => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return (
    [year, month, day].map(formatNumber).join('/') +
    ' ' +
    [hour, minute, second].map(formatNumber).join(':')
  )
}
export const formatTimeYMD= (date: Date) => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  return (
    [year, month, day].map(formatNumber).join('-')
  )
}
const formatNumber = (n: number) => {
  const s = n.toString()
  return s[1] ? s : '0' + s
}

/**
 * @class 防抖封装
 */
export class Debounce {
  public use = (delay: number): Function => {
    let timer: number | undefined = undefined;
    return (): Promise<void> => {
      return new Promise((resolve, reject) => {
        if (timer) {
          clearTimeout(timer);
        }
        timer = setTimeout(() => {
          resolve();
          timer = undefined;
        }, delay)
      })
    }
  }
}
/**
 * @function 节流封装
 */
export function Throttle() {
  let timer: number | undefined = undefined;
  return (delay: number): Promise<void> => {
    return new Promise((resolve, reject) => {
      if (timer) {
        timer = setTimeout(() => { }, delay);
        reject();
      }
      else {
        resolve();
        timer = setTimeout(() => {
          if (timer) {
            clearTimeout(timer);
            timer = undefined;
          }
        }, delay)
      }
    })
  }
}

/**
 * @function 定位封装
 */
export function checkLocation(lat2:number,lon2:number,radius:number){
  return new Promise((resolve,reject)=>{
    wx.getLocation({
      type:'gcj02',
      isHighAccuracy:true,
      highAccuracyExpireTime:5000,
      success(res){
        let lat = res.latitude;
        let lon = res.longitude;
        //let accuracy = res.accuracy精确度
        let distance = _calDistance(lat,lon,lat2,lon2);
          
        if(distance<=radius) resolve({res:true,distance:distance});
        else {
          msg.show('地理位置不在签到范围内','距离签到点'+distance+'米，要求'+radius+'米内。定位有误差，请多次尝试');
          resolve({res:false,distance:distance});
        }
      },
      fail(err){
        reject(err);
      }
    })
  })
}
function _calDistance(lat1:number,lng1:number,lat2:number,lng2:number){
    //console.log(3);
    let s1=lat1;
    let s2=lng1;
    //x.innerHTML="纬度: " + s1 + ",经度: " + s2;
    
    var EARTH_RADIUS = 6370.996; // 地球半径系数
    var PI = 3.1415926; 
    var radLat2 = lat2;
    var radLng2 = lng2;
    var radLat1 = s1 * PI / 180.0;
     radLat2 = radLat2 * PI / 180.0;
 
    var radLng1 = s2 * PI / 180.0;
     radLng2 = radLng2 * PI /180.0;

    var a = radLat1 - radLat2;
    var b = radLng1 - radLng2;

    let distance_temp = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b/2),2)));
    distance_temp = distance_temp * EARTH_RADIUS * 1000;
    return Math.round(distance_temp);
}